package com.yangchoi.jetpackcompose.ui.main.page

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.loren.component.view.composesmartrefresh.SmartSwipeStateFlag
import com.loren.component.view.composesmartrefresh.rememberSmartSwipeRefreshState
import com.yangchoi.jetpackcompose.route.RouteKey
import com.yangchoi.jetpackcompose.route.RouteNavigationUtil
import com.yangchoi.jetpackcompose.theme.black_16Sp
import com.yangchoi.jetpackcompose.theme.white_16sp
import com.yangchoi.jetpackcompose.ui.baseui.BaseComposePage
import com.yangchoi.jetpackcompose.ui.main.viewmodel.ProjectEvent
import com.yangchoi.jetpackcompose.ui.main.viewmodel.ProjectViewModel
import com.yangchoi.jetpackcompose.ui.widget.component.CommonTopBar
import com.yangchoi.jetpackcompose.ui.widget.component.SmartColumnWidget
import com.yangchoi.jetpackcompose.ui.widget.ext.onClick
import com.yangchoi.jetpackcompose.utils.Base64Util

@Composable
fun ProjectPage(navHostController: NavHostController,viewModel: ProjectViewModel = hiltViewModel()){

    val viewState = viewModel.viewState
    val treeList = viewState.treeList
    val projectList = viewState.projectList

    var selectIndex by remember { mutableStateOf(0) }
    var currentPage by remember { mutableStateOf(1) }
    val refreshState = rememberSmartSwipeRefreshState()

    BaseComposePage(
        topBarContent = {
            CommonTopBar(title = "项目", showBackIcon = false)
        },
        onResume = {
            viewModel.dispatch(event = ProjectEvent.OnInitProject,currentPage = currentPage)
        }
    ) {
        treeList?.let {
            LazyRow(modifier = Modifier
                .padding(horizontal = 12.dp)
                .fillMaxWidth()){
                itemsIndexed(treeList){ index, item ->
                    Box(modifier = Modifier
                        .padding(end = 16.dp)
                        .width(IntrinsicSize.Max)
                        .onClick {
                            currentPage = 1
                            selectIndex = index
                            viewModel.dispatch(event = ProjectEvent.OnGetProjectById, currentPage = currentPage, cid = item.id,onRefreshFinish = {
                                refreshState.refreshFlag = SmartSwipeStateFlag.SUCCESS
                            })
                        }) {
                        Text(text = "${item.name}", style = black_16Sp.copy(fontSize = 18.sp))
                        Spacer(
                            modifier = Modifier
                                .padding(start = 5.dp, end = 5.dp, top = 26.dp)
                                .fillMaxWidth()
                                .height(2.dp)
                                .background(
                                    color = if (selectIndex == index) Color(0xffFFC300) else Color.White,
                                    shape = RoundedCornerShape(2.dp)
                                )
                        )
                    }
                }
            }
        }

        SmartColumnWidget(
            state = refreshState,
            onRefresh = {
                Log.e("RefreshTAG","触发刷新")
                currentPage = 1
                viewModel.dispatch(event = ProjectEvent.OnUpdatePage, currentPage = currentPage, onRefreshFinish = {
                    Log.e("RefreshTAG","结束回调")
                    refreshState.refreshFlag = SmartSwipeStateFlag.SUCCESS
                })
            },
            onLoadMore = {
                currentPage++
                viewModel.dispatch(event = ProjectEvent.OnUpdatePage, currentPage = currentPage, onRefreshFinish = {
                    refreshState.loadMoreFlag = SmartSwipeStateFlag.SUCCESS
                })
            },
            content = {
                projectList?.let {
                    itemsIndexed(projectList){ index, value ->
                        HomeArticleItem(item = value){ item ->
                            RouteNavigationUtil.navigationTo(navHostController,
                                RouteKey.WEB_VIEW.toString(), args = Base64Util.stringToBase64("${item.link}"))
                        }
                    }
                }
            }
        )
    }
}